import json
import datetime
import os
from collections import Counter
import csv
import time
import datetime
ht=[ '#YearOnTikTok', '#lovestory','#artmas','#festivefashion','#happyhanukkah','#planttiktok','#productivity','#ImAGhost','#catchphrases','#ootd','#comfortfood','#holidaycrafts','#watchmegrow','#holidaydecor','#raisedby','#makeitvogue','#goodmorning','#watchingxfactor','#catchphrases','#rnbvibes','#gamenight','#HorrorToK', '#ProblemSolved', '#SkateLife', '#ThriftShop', '#SpookyTreats',  '#tiktokfood', '#interiordesign', '#sfxmakeup', '#ComingOfAge', '#falldiy', '#worldseries', '#gaminglife', '#meleaving', '#yellow', '#yougotthis','#mycostume','#mypfp','#welldone','#nativefamily','#happyhalloween','#homeoffice','#myrecommendation','#myhobby','#fanedit','#stemlife','#motivationmonday','#wip','#veteransday','#holidaycountdown','#WeekendVibes','#holidaysourway','#RoomTour','#bakingszn','#nbadraft',
     '#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#fallguysmoments','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok','#holidaymusic','#howbizarre', '#personalfinance'
     ,'#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion','graphicdesign','smallbusiness','#halloweenlook','fallfashion','#artmas','#haventseen','#planttiktok','#productivity','#bekind','#onhold','#growupwithme','#tabletop','#theatrekids','#wishlist','#haventseen','#selfimprovement', '#OhNo','#animation']
ht=[h.lower().replace('#','') for h in ht]
dict={}
with open('D:\\Work\\Tool\\tiktok\\age.tsv', 'w', encoding='utf-8',
          newline='\n') as filename_output:
    writer = csv.writer(filename_output, delimiter='\t')
    writer.writerow(['Hashtag','First Appearance','Challenge First Appearance','First Day on Trending'])
    for hashtag in ht:
        c=Counter()
        datalist=[]

        for name in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
            if hashtag in name.lower():
                datalist.append(name)
        dict[hashtag] = [datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'),
                         datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'), datalist[0].split('_')[2]]
        for i in range(len(datalist)):
            with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + datalist[i], 'r', encoding='utf-8', newline='\n') as filename_input:
                for line in filename_input:
                    z = json.loads(line)
                    if 'id' in z.keys():
                        if time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['createTime'])))<dict[hashtag][0]:
                            dict[hashtag][0]=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['createTime'])))
                        f=False
                        for item in z['challenges']:
                            if hashtag in item['title'].lower():
                                f=True
                                break
                        if time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['createTime'])))<dict[hashtag][1] and f :
                            dict[hashtag][1]=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['createTime'])))
                    elif 'itemInfos' in z.keys():
                        if time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['itemInfos']['createTime'])))<dict[hashtag][0]:
                            dict[hashtag][0] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['itemInfos']['createTime'])))
                        f = False
                        for item in z["challengeInfoList"]:
                            if hashtag in item["challengeName"].lower():
                                f = True
                                break
                        if time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['itemInfos']['createTime']))) < dict[hashtag][1] and f:
                            dict[hashtag][1] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(z['itemInfos']['createTime'])))
        print(hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2])
        writer.writerow([hashtag,dict[hashtag][0],dict[hashtag][1],dict[hashtag][2]])